package com.nuc.zyt.day04.demo.GrabRedEnvelopes;

import java.util.Random;

/**
 * @ClassName RedEnvelopes
 * @Description TODO
 * @Author 耀酱
 * @Date 2023/5/30 15:22
 * @Version 1.0
 */

public class RedEnvelopes extends Thread{

    static double number = 100.0; // 红包总金额
    static int count = 3; // 红包个数

    static final double MinValue = 0.01;


    @Override
    public void run() {
        while (true){
            synchronized (RedEnvelopes.class){
                double value = 0; // 中奖金额
                // 判断是否还有红包
                if(count == 0){
                    System.out.println(getName()+"没有抢到红包");
                }else {
                    // 还有红包
                    if(count == 1){
                        // 最后一个红包
                        value = number;
                    }else {
                        //double v = number - (count - 1) * MinValue;
                        System.out.println(MinValue + new Random().nextDouble() * (number - (count - 1) * MinValue));
                        value = MinValue + new Random().nextDouble() * (number - (count - 1) * MinValue);
                    }
                    number = number - value;
                    count--;
                    System.out.println(getName()+"抢到了"+value+"元的红包");


                }
            }
        }

    }
}
